Ext.define('Holo.controller.MainMenu', {
    extend: 'Ext.app.Controller',
	
    views: [
        'MainMenu',
        'Mainstreet'
    ],
    
    stores: [
        'Users',
        'Apps',
        'Spots',
        'Trips'
    ],
    
    
    refs: [
            {
               selector: '#main',
               ref: 'main'
            },{
                selector :'mainmenu button[action=createspot] menu',
                ref:'menuSpot'
            },{
                selector :'mainmenu button[action=createtrip] menu',
                ref:'menuTrip'
            },{
                selector :'mainmenu button[action=createapp] menu',
                ref:'menuApp'
            }
    ],



    
    init: function() {
    	 this.control({
        
	        'mainmenu button[action=createspot]': {
                    click : this.showWindowsCreateSpot		
				
		 },
                 
                'mainmenu button[action=createspot] menu': {
                    click : function(menu, item){
                            GlobalData.currentSpot =  this.getSpotsStore().getById(item.idspot)                            
                            this.showSpotManager();
                    }		
				
		 },
                
                'mainmenu button[action=createtrip]': {
                    click : this.showWindowsCreateTrip		
				
		 },
                 
                'mainmenu button[action=createtrip] menu': {
                    click : function(menu, item){
                            GlobalData.currentTrip =  this.getTripsStore().getById(item.idtrip)                            
                            this.showTripManager();
                    }		
				
		 },
                
                'mainmenu button[action=createapp]': {
                    click : this.showWindowsCreateApp		
				
		 },
                 
                'mainmenu button[action=createapp] menu': {
                    click : function(menu, item){
                            GlobalData.currentApp = this.getAppsStore().getById(item.idapp)                            
                            this.showAppManager();
                     }
                 },
                
                 
                'mainmenu button[action=createformuserdesc]': {
                    click : this.showUserManager
                 }
            
                	 
	     });
    },

    
    
        
    showWindowsCreateSpot : function(){
        var win = Ext.create('Holo.view.spot.WindowsNewSpot', {
                               renderTo: Ext.getBody()
                         });
    
        win.show();
    },
    
    
    
    showWindowsCreateTrip : function(){
        var win = Ext.create('Holo.view.trip.WindowsNewTrip', {
                               renderTo: Ext.getBody()
                         });
    
        win.show();
    },
    
    showWindowsCreateApp : function(){
        var win = Ext.create('Holo.view.app.WindowsNewApp', {
                               renderTo: Ext.getBody()
                         });
    
        win.show();
    },
    
    
    
     /**
     * Affect the manage spot view to the main and create the SpotDescriptions
     * Is alway the GobalCurrentSpot who's get the focus
     * @returns void
     */    
     showSpotManager : function(){
       
        //Define the current SpotDescriptionsLabel store  
        var spotDescriptionsLabel = Ext.create('Holo.store.SpotDescriptions', {
                filters: [{
                    property: 'spot_id',
                    value   :  GlobalData.currentSpot.get('id')
                },{property: 'type',
                    value   :  'L'
                }]    
            });
       
        Ext.data.StoreManager.replace('SpotDescriptionsLabel', spotDescriptionsLabel);
       
       
       	var spotDescriptionsContent= Ext.create('Holo.store.SpotDescriptions', {
                filters: [
		    {property: 'spot_id',value   :  GlobalData.currentSpot.get('id')},
		    {filterFn: function(item) { return item.get("type") !="L"; }}
		] 
	 });
	
	Ext.data.StoreManager.replace('SpotDescriptionsContent', spotDescriptionsContent);
      

        
        var panManageSpotContent = Ext.create('Holo.view.ManagerSpot', {
            storeLabel : spotDescriptionsLabel,
	    storeContent : spotDescriptionsContent
        });

    
        //Show ManageSpotDescription
        this.getMain().removeAll();
        this.getMain().add(panManageSpotContent);
    
    },
    
    /**
     * Affect the manage Trip view to the main and create the TipDescriptions
     * Is alway the GobalCurrentTrip who's get the focus
     * @returns void
     */    
     showTripManager : function(){
       
        //Define the current TripDescriptionsLabel store  
        var storeTripDescriptions = Ext.create('Holo.store.TripDescriptions', {
                filters: [{
                    property: 'trip_id',
                    value   :  GlobalData.currentTrip.get('id')
                }]    
            });
       
        Ext.data.StoreManager.replace('TripDescriptions', storeTripDescriptions);
        
        
        var storeTripSpots = Ext.create('Holo.store.TripSpots', {
                filters: [{
                    property: 'trip_id',
                    value   :  GlobalData.currentTrip.get('id')
                },{
                    property: 'user_id',
                    value   :  GlobalData.currentUserId
                }]    
            });
       
        Ext.data.StoreManager.replace('TripSpots', storeTripSpots);
        
        
        controller = this;
        storeTripSpots.load({
            
            callback : function(){
                //Show ManageTripDescription
                controller.getMain().removeAll();
                
                var panManageTripContent = Ext.create('Holo.view.ManagerTrip', {
                    storeLabel : storeTripDescriptions,
                    storeSpotLinks : controller.getSpotsStore()
                });
                
                controller.getMain().add(panManageTripContent);
            }
        });
        
    },
    
    

    /**
     * Affect the manage app view to the main and create the AppDescriptions
     * Is alway the GobalCurrentApp who's get the focus
     * @returns void
     */    
     showAppManager : function(){
       
        this.getMain().removeAll();
         
        //console.log( "Current app id : "+ GlobalData.currentApp.get('id'));
        var storeAppTrips = Ext.create('Holo.store.AppTrips', {
                filters: [{
                    property: 'app_id',
                    value   :  GlobalData.currentApp.get('id')
                },{
                    property: 'user_id',
                    value   :  GlobalData.currentUserId
                }]    
            });
       
        Ext.data.StoreManager.replace('AppTrips', storeAppTrips);
        
    
    
        var controller = this;
        storeAppTrips.load({
            
            callback : function(){
               
                var panManageAppContent = Ext.create('Holo.view.ManagerApp', {
                    storeTripLinks : controller.getTripsStore()
                });
                
                controller.getMain().add(panManageAppContent);
            }
        });
            
    
    },
    
    showUserManager : function(){
     
        this.getMain().removeAll();
        
        var panManageUserContent = Ext.create('Holo.view.ManagerUser', {});
        
        this.getMain().add(panManageUserContent);
    
    },
    
    
    /**
     * Set all avalaible spot items into the select button of the main menu
     * Also use for the refresh
     * @returns void
     */
    setItemsToSelectSpotMenu : function(){
        var menu = this.getMenuSpot()
        menu.removeAll();
        menu.setDisabled(true);
        
        storeSpot = this.getSpotsStore();
        //bug... add two time when create new spot is calling
        //if (storeSpot.count()>1)
          //      storeSpot.sort('name', 'ASC');
        
        var fn = function(currentSpot){
                    //the scope is the menu of the button
                    this.add({ text : currentSpot.get('name'), idspot:currentSpot.get('id')});
                 };
        storeSpot.each(fn, menu);
        menu.setDisabled(false);
        
    },
    
    /**
     * Set all avalaible trip items into the trip select button of the main menu
     * Also use for the refresh
     * @returns void
     */
    setItemsToSelectTripMenu : function(){
        var menu = this.getMenuTrip()
        menu.removeAll();
        menu.setDisabled(true);
        
        storeTrips = this.getTripsStore();
        //bug... add two time when create new spot is calling
        //if (storeSpot.count()>1)
          //      storeSpot.sort('name', 'ASC');
        
        var fn = function(currentTrip){
                    //the scope is the menu of the button
                    this.add({ text : currentTrip.get('name'),
                               idtrip:currentTrip.get('id')
                            });
                 };
        storeTrips.each(fn, menu);
        menu.setDisabled(false);
    },
    
    
    /**
     * Set all avalaible trip items into the trip select button of the main menu
     * Also use for the refresh
     * @returns void
     */
    setItemsToSelectAppMenu : function(){
        var menu = this.getMenuApp()
        menu.removeAll();
        menu.setDisabled(true);
        
        storeApps = this.getAppsStore();
        //bug... add two time when createNewspot() is calling
        //if (storeSpot.count()>1)
          //      storeSpot.sort('name', 'ASC');
        var fn = function(currentApp){
                    //the scope is the menu of the button
                    this.add({ text : currentApp.get('name'),
                               idapp:currentApp.get('id')
                            });
                 };
        storeApps.each(fn, menu);
        menu.setDisabled(false);
    }
    
    
    
});